package com.syni.mdd.sjb.common.component.utils.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class BeanUtils extends org.springframework.beans.BeanUtils {


    public static void populate(final Object bean, final Map<String, ? extends Object> properties) {
        try {
            org.apache.commons.beanutils.BeanUtils.populate(bean, properties);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static <T> List<T> copyList(List list,Class<T> classes){
        List<T> targets = new ArrayList<>();
        try {
            if(!(list != null && list.size() > 0)){
                return new ArrayList();
            }
            for(Object obj : list){
                T target = classes.newInstance();
                BeanUtils.copyProperties(obj,target);
                targets.add(target);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return targets;
    }

    public static <T> List<T> copyListIgnore(List list,Class<T> classes,String ...outWord){
        List<T> targets = new ArrayList<>();
        try {
            if(!(list != null && list.size() > 0)){
                return new ArrayList();
            }
            for(Object obj : list){
                T target = classes.newInstance();
                BeanUtils.copyProperties(obj,target,outWord);
                targets.add(target);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return targets;
    }


    public static <T> T copyProperties(Object object,Class<T> classes){
        try {
            T target = classes.newInstance();
            BeanUtils.copyProperties(object,target);
            return target;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void setArgs(Object obj, String[] arg, Object origin, Object val) {
        Set args = new HashSet(Arrays.asList(arg));
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.get(obj) == origin) {
                    if (args.contains(field.getName())) {
                        field.set(obj, val);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    public static Map<String, Object> objectToMap(Object obj) {
        return JSONObject.parseObject(JSONObject.toJSONString(obj), Map.class);
    }

    public static boolean checkPropertiesNull(Object obj, String... properies) {
        Integer trueNum = 0;

        if (properies.length > 0) {
            for (String str : properies) {
                try {
                    Field field = obj.getClass().getDeclaredField(str);
                    if (field != null) {
                        if (str.equals(field.getName())) {
                            field.setAccessible(true);
                            if (field.get(obj) != null && StringUtils.isNotBlank(field.get(obj).toString())) {
                                trueNum++;
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return trueNum == properies.length;
    }

    /**
     * @author: huangwenquan
     * @date:  2020/10/9
     * @description:遍历一个对象的属性 将非空属性赋值给另一个对象
     */
    public static <T> void mergeObject(T origin, T destination,String ...outWord) {
        if (origin == null || destination == null)
            return;
        if (!origin.getClass().equals(destination.getClass()))
            return;
        List<String> tempList = Arrays.asList(outWord);
        Field[] fields = origin.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                String name = fields[i].getName();
                if(!tempList.contains(name)){
                    fields[i].setAccessible(true);
                    Object value = fields[i].get(origin);
                    Object desValue = fields[i].get(destination);
                    if (null != value && (desValue == null || desValue.toString().isEmpty())) {
                        fields[i].set(destination, value);
                    }
                    fields[i].setAccessible(false);
                }
            } catch (Exception e) {
            }
        }
    }
}
